<?php

namespace addons\reqlog;

use app\common\library\Menu;
use think\Addons;

/**
 * 插件
 */
class Reqlog extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $menu = [  
            [  
                'name'    => 'reqlog',  # 菜单英文名称
                'title'   => '请求统计', # 菜单展示标题
                'icon'    => 'fa fa-calendar-minus-o', # 图标 
                'sublist' => [ # 子菜单,可根据此格式向下扩展
                    [
                        'name' => 'reqlog/mca', 
                        'title' => '请求配置',
                        'icon' => 'fa fa-sliders',
                        'sublist' => [
                            ['name' => 'reqlog/mca/index', 'title' => '查看'],
                            ['name' => 'reqlog/mca/edit', 'title' => '编辑'],
                            ['name' => 'reqlog/mca/del', 'title' => '删除'],
                        ]
                    ], 
                    [
                        'name' => 'reqlog/log', 
                        'title' => '请求记录',
                        'icon' => 'fa fa-calendar',
                        'sublist' => [
                            ['name' => 'reqlog/log/index', 'title' => '查看'],
                            ['name' => 'reqlog/log/edit', 'title' => '编辑'],
                            ['name' => 'reqlog/log/del', 'title' => '删除'],
                        ]
                    ]
                    
                ]
            ]
        ];
        Menu::create($menu);        
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete('reqlog');
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        Menu::enable('reqlog');
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable('reqlog');
        return true;
    }

    /**
     * 控制器开始标签位
     *
     * @return void
     * @author AriFe.Liu 
     */
    public function actionBegin(){
        //当前模块名称
        $m=request()->module();
        //当前控制名称
        $c=request()->controller();
        //当前类方法名称
        $a=request()->action();
        $mca = strtolower(implode('/',[$m,$c,$a]));
        // echo $mca;die;
        $mca_list = $this->getMcaList();
        if(isset($mca_list[$mca])){
            $v = $mca_list[$mca];
            $client = $this->getClientInfo($m);
            db('ReqlogLog')->insert([
                'title' => $v['title'],
                'm' => $v['m'],
                'c' => $v['c'],
                'a' => $v['a'],
                'mca' => $v['mca'],
                'client_id' => $client['id'],
                'client_type' => $client['type'],
                'ip' => $_SERVER['REMOTE_ADDR'],
                'createtime' => time()
            ]);
        }
    }

    /**
     * 获取请求配置列表
     *
     * @return void
     * @author AriFe.Liu 
     */
    private function getMcaList(){
        $mca_list = db('ReqlogMca')
            ->where(['status'=>'1'])
            ->cache('MCA_LIST',100)
            ->column("LOWER(mca) mca,title,m,c,a");
        return $mca_list;
    }

    /**
     * 获取用户ID, 可能是前台用户ID,也可能是后台管理员ID!
     *
     * @param [type] $m
     * @return void
     * @author AriFe.Liu 
     */
    private function getClientInfo($m){
        $client = [
            'id' => 0,
            'type' => ''
        ];
        if($m == 'admin'){ // 后台
            $auth = \app\admin\library\Auth::instance();
            $client['type'] = 'admin';
            $client['id'] = $auth->id;
        }else{
            //获取Auth对象
            $auth = \app\common\library\Auth::instance();
            //获取会员模型
            $user = $auth->getUser();
            $client['type'] = 'user';
            $client['id'] =  ($user && $user->id) ? $user->id : 0;
        }
        return $client;
    }
    
}
